<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_NEWYEAR.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_NEWYEAR,$login['staff_auth']);

$dosearch = FALSE ;

$dao = new Class_M_NEWYEAR;


if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['NEWYEAR'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['NEWYEAR'] = $forms;
}

$wherearr =  array();

/*
if ( isset( $forms['newyear_cd'] ) &&  ( !empty( $forms['newyear_cd'] ) ) ){
	$wherearr[] = sprintf(" newyear_cd =  '%s' ",  $forms['newyear_cd'] );
}
*/
if ( isset( $forms['year_mark'] ) &&  ( !empty( $forms['year_mark'] ) ) ){
	if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$wherearr[] = sprintf(" a.year_mark like  '%s' ",  "%".$forms['year_mark']."%" );
	} else {
		$wherearr[] = sprintf(" a.year_mark like  '%s' ",  "%".$forms['year_mark']."%" );
	}
}
/*
if ( isset( $forms['media_flg']  )  &&  ( !empty( $forms['media_flg'] ) ) ){
	$wherearr[] = sprintf(" media_flg =  '%s' ",  $forms['media_flg'] );
}
*/


//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "newyear".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr );
	$colums = array();
	
	$col_heads = array(
		'year_mark'    => array( '記号',  1),
		'd1229'        => array( '12月29日',  1),
		'd1230'        => array( '12月30日',  1),
		'd1231'        => array( '12月31日',  1),
		'd0101'        => array( '1月1日',  1),
		'd0102'        => array( '1月2日',  1),
		'd0103'        => array( '1月3日',  1),
		'd0104'        => array( '1月4日',  1),
		'memo_day1'    => array( '告知チラシ週名1',  1),
		'memo_text1'   => array( '告知内容1',  1),
		'memo_day2'    => array( '告知チラシ週名2',  1),
		'memo_text2'   => array( '告知内容2',  1),
		'memo_day3'    => array( '告知チラシ週名3',  1),
		'memo_text3'   => array( '告知内容3',  1),
		'memo_day4'    => array( '告知チラシ週名4',  1),
		'memo_text4'   => array( '告知内容4',  1),
		'shop_cd'      => array( '店舗コード',  1),
		'shop_name'    => array( '店舗',  1),
		'u_time'       => array( '更新日時',        3),
		'c_time'       => array( '登録日時',        3),
		'm_user_name'  => array( '更新者',          1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
}

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);


$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mnewyear_index.html');

?>